import logging

import pytest

from resource.pom.gallery import Gallery
from utils.manager import MatchRules


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：预置图片资源------')
    app = Gallery
    device.mediatool_delete_all()
    device.hdc_file_send(app.photo1, app.photo_dir)
    device.mediatool_send(app.photo_dir)
    yield
    logging.info('------用例后置：删除预置资源------')
    device.mediatool_delete_all()


class Test:
    app = Gallery

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('预置条件：烧写目标版本后，正常进入桌面-图库应用，已新建相册')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.assert_current_window(self.app.window_name)
        device.refresh_layout()
        # 照片
        tab_photos = device.get_element_by_key('TabPhotos')
        # 相册
        tab_albums = device.get_element_by_key('TabAlbums')
        device.click_element(tab_albums)
        device.refresh_layout()
        device.click_element(device.get_element_by_key('ActionBarButtonNew'))
        device.refresh_layout()
        new_album_name = device.get_element_by_key('NewAlbumDialogTextInput').get('text')
        device.click_element(device.get_element_by_text('确定'))
        device.go_back()

        logging.info('步骤1.点击打开图库')
        logging.info('预置条件已打开，无需重新打开')
        logging.info('预期结果1.正常进入图库应用内')
        logging.info('预置条件已检查，无需重复检查')

        logging.info('步骤2.点击图片')
        device.click_element(tab_photos)
        device.refresh_layout()
        org_photo = device.get_element_by_key('ImageGridFocus_file', rule=MatchRules.Contain)
        device.click_element(device.get_element_by_key('Gesture_1'))
        logging.info('预期结果2.正常进入图片详情页')
        device.refresh_layout()
        device.assert_key_exist('PhotoBrowserBg')
        device.assert_key_exist('ActionBarButtonBack')
        device.assert_key_exist('PhotoBrowserDateTitle')
        device.assert_key_exist('DetailMenuPanel')
        device.assert_key_exist('ActionBarButtonInfo')
        device.assert_key_exist('ToolBarButtonFavor')
        device.assert_key_exist('ToolBarButtonEdit')
        device.assert_key_exist('ToolBarButtonDelete')
        device.assert_key_exist('ToolBarButtonMore')

        logging.info('步骤3.点击右下角更多按钮')
        device.click_element(device.get_element_by_key('ToolBarButtonMore'))
        logging.info('预期结果3.正常弹出选项框')
        device.refresh_layout()
        device.assert_text_exist('添加到相册')
        device.assert_text_exist('重命名')

        logging.info('步骤4.选择添加到相册')
        device.click_element(device.get_element_by_text('添加到相册'))
        logging.info('预期结果4.成功跳转到相册列表页')
        device.refresh_layout()
        device.assert_key_exist('MediaOperationPageAlbum0')
        device.assert_text_exist(new_album_name)

        logging.info('步骤5.选择一个相册')
        device.click_element(device.get_element_by_text(new_album_name))
        device.go_back()
        logging.info('预期结果5.成功跳转到相册内且图片成功添加到相册内')
        device.click_element(tab_albums)
        device.refresh_layout()
        device.click_element(device.get_element_by_key('Album_1'))
        device.refresh_layout()
        added_photo = device.get_element_by_key('ImageGridFocus_file', rule=MatchRules.Contain)
        assert org_photo.get('text') == added_photo.get('text'), '图片/视频添加到相册失败'
